Attribute VB_Name = "modPushParamThunk"
'***************************************************************
' (c) Copyright 2000 Matthew J. Curland
'
' This file is from the CD-ROM accompanying the book:
' Advanced Visual Basic 6: Power Techniques for Everyday Programs
'   Author: Matthew Curland
'   Published by: Addison-Wesley, July 2000
'   ISBN: 0-201-70712-8
'   http://www.PowerVB.com
'
' You are entitled to license free distribution of any application
'   that uses this file if you own a copy of the book, or if you
'   have obtained the file from a source approved by the author. You
'   may redistribute this file only with express written permission
'   of the author.
'
' This file depends on:
'   References:
'     None
'   Files:
'     None
'   Minimal VBoost conditionals:
'     None
'   Conditional Compilation Values:
'     PUSHPARAM_NONORMAL = 1 'Remove support for PushParamThunk
'     PUSHPARAM_NOSTACKRETURN = 1 'Remove support for PushParamThunkStackReturn
'
' This file is discussed in Chapter 11.
'***************************************************************
Option Explicit

#Const NORMALSUPPORT = PUSHPARAM_NONORMAL = 0
#Const STACKRETURNSUPPORT = PUSHPARAM_NOSTACKRETURN = 0

#If NORMALSUPPORT Then
Private Type ThunkBytes
    Thunk(5) As Long
End Type
Public Type PushParamThunk
    pfn As Long
    Code As ThunkBytes
End Type
#End If

#If STACKRETURNSUPPORT Then
Private Type ThunkBytesStackReturn
    Thunk(7) As Long
End Type
Public Type PushParamThunkStackReturn
    pfn As Long
    Code As ThunkBytesStackReturn
End Type
#End If

#If NORMALSUPPORT Then
Public Sub InitPushParamThunk(Thunk As PushParamThunk, ByVal ParamValue As Long, ByVal pfnDest As Long)
'push [esp]
'mov eax, 16h // Dummy value for parameter value
'mov [esp + 4], eax
'nop // Adjustment so the next long is nicely aligned
'nop
'nop
'mov eax, 1234h // Dummy value for function
'jmp eax
'nop
'nop
    
    With Thunk.Code
        .Thunk(0) = &HB82434FF
        .Thunk(1) = ParamValue
        .Thunk(2) = &H4244489
        .Thunk(3) = &HB8909090
        .Thunk(4) = pfnDest
        .Thunk(5) = &H9090E0FF
    End With
    Thunk.pfn = VarPtr(Thunk.Code)
End Sub
#End If

#If STACKRETURNSUPPORT Then
Public Sub InitPushParamThunkStackReturn(Thunk As PushParamThunkStackReturn, ByVal ParamValue As Long, ByVal pfnDest As Long)
'push [esp]
'mov eax, [esp + 8]
'mov [esp + 4], eax
'mov eax, 16h // Dummy value for parameter value
'mov [esp + 8], eax
'nop // Adjustment so the next long is nicely aligned
'nop
'nop
'mov eax, 1234h // Dummy value, function goes here
'jmp eax
'nop
'nop

    With Thunk.Code
        .Thunk(0) = &H8B2434FF
        .Thunk(1) = &H89082444
        .Thunk(2) = &HB8042444
        .Thunk(3) = ParamValue
        .Thunk(4) = &H8244489
        .Thunk(5) = &HB8909090
        .Thunk(6) = pfnDest
        .Thunk(7) = &H9090E0FF
    End With
    Thunk.pfn = VarPtr(Thunk.Code)
End Sub
#End If
